/*
CpGears v1.0

Copyright (c) 2010 Yves Riel http://www.flash-factor.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cpgears.helpers {
	/**
	 * Helper class to manipulate strings.
	 * 
	 * @author Whyves
	 */
	public class StringUtil {
		
		/**
		 * Formats a string by replacing place holders with actual values.
		 * 
		 * <P>The string can contains place holders which are integer(s) enclosed
		 * in curly braces e.g. <code>{0]</code>. Each place holder is then replaced with
		 * a value. You can have as many place holders as you have values.</P>
		 * 
		 * <P>The function call <code>format("My name is {0} and this project is called {1}.", "Yves", "CpGears");</code>
		 * will return "My name is Yves and this project is called Cpgears".
		 * </P>
		 * 
		 * 
		 * @param	str The string to format.
		 * @param	...rest The values that are inserted into the place holders.
		 * @return The formatted string.
		 */
		public static function format(str:String, ...rest):String {
			if (str == null) return '';
			
			// Replace all of the parameters in the msg string.
			var numberOfToken:uint = rest.length;
			var tokens:Array;
			if (numberOfToken == 1 && rest[0] is Array)
			{
				tokens = rest[0] as Array;
				numberOfToken= tokens.length;
			}
			else
			{
				tokens = rest;
			}
			
			for (var i:int = 0; i < numberOfToken; i++)
			{
				str = str.replace(new RegExp("\\{"+i+"\\}", "g"), tokens[i]);
			}

			return str;
		}

		/**
		 * Tests for a null or empty string.
		 * 
		 * @param	string The string to test
		 * @return <code>true</code> if the string is null or empty, <code>false</code> otherwise.
		 */
		public static function isNullOrEmpty(string:String):Boolean {
			return (string == null || string == "");
		}
	}
}